﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyHtmlEditor
{
    public partial class InsertLink : Form
    {
        public string url = "";
        public string text = "";
        public string initialDirectory;

        public InsertLink()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 1;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            finish();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.DefaultExt = ".html";
            if (comboBox1.Text == "file:///")
            {
                ofd.Filter = "HTML files (*.html)|*.html";
                ofd.InitialDirectory = initialDirectory;
            }
            else //if (comboBox1.SelectedText == "shell://")
            {
                ofd.Filter = "All files (*.*)|*.*";
            }

            ofd.Multiselect = false;
            if (ofd.ShowDialog() != DialogResult.OK)
                return;
            textBox1.Text = ofd.FileName;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                finish();
        }

        private void finish()
        {
            if (textBoxLinkText.Text == "")
                text = textBox1.Text;
            else
                text = textBoxLinkText.Text;

            if (comboBox1.Text == "shell:")
                url = comboBox1.Text + textBox1.Text.Replace("/", "%%FSLASH%%");
            else
                url = comboBox1.Text + textBox1.Text;
            url = url.Replace( "\"", "%%QUOTE%%");
            
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
